<?php

require_once(DIR_LIB."vslibdef.php");

if(!defined('DIR_LIB_VSCORE'))
	define('DIR_LIB_VSCORE', DIR_LIB."VsCore".DIR_SEP);

require_once(DIR_LIB_VSCORE."db.php");

if(defined("VS_MULTILANG"))
	require_once(DIR_LIB_VSCORE."vslocale.php");

require_once(DIR_LIB."VsShop".DIR_SEP."vsproduct.php");

class VsProductCategory {

	var $_db = NULL;
	var $_sqlTable='products_categories';

	var $_id = 0;
	var $_name = '';
	var $_date='';

	var $_parent = NULL;
	var $_subcategories;

	private $_path = NULL;		// Category Path from Root

	private $_childCount = NULL;		// Broj na podkategoriite
	private $_productCount = NULL;
	private $_productCountTotal = NULL;		// i v podkategoriite

	var $_orderBy = 0;
	var $_enabled = FALSE;
	var $_global = FALSE;		// Dali e globalna konstantna kategoriq

	function VsProductCategory() {
		$this->_sqlTable = $this->_sqlTable.strtolower(VsSession::Instance()->value('lang'));
		$this->_db = VsDatabase::Instance();

		//$this->_parent = $parent;
	}
	//==============================================================================

	function install() {


	}
	//==============================================================================

	function id() {
		return $this->_id;
	}
	//=============================================================================

	function parentId(){
		return $this->_parent;
	}
	//==============================================================================

	function name() {
		return $this->_name;
	}
	//=============================================================================

	function path() {
		if($this->_path === NULL) {
			$this->_path = array();
			$this->initPath($this->_parent);
		}

		return $this->_path;
	}
	//===============================================================================

	function childCount() {
		if($this->_childCount === NULL) {
			$this->_childCount = 0;
			$this->initChildCount();
		}

		return $this->_childCount;
	}
	//===============================================================================

	function productCount() {
		if($this->_productCount === NULL) {
			$this->_productCount = 0;
			$this->initProductCount();
		}

		return $this->_productCount;
	}
	//===============================================================================

	function productCountTotal() {
		if($this->_productCountTotal === NULL) {
			$this->_productCountTotal = 0;
			$this->initProductCountTotal();
		}

		return $this->_productCountTotal;
	}
	//===============================================================================

	function orderBy() { return $this->_orderBy; }
	function enabled() { return $this->isEnabled(); }
	function isEnabled() { return $this->_enabled ? TRUE : FALSE; }
	//==============================================================================

	function save() {
		if($this->_id > 0)
			$this->update();
		else
			$this->insert();
	}
	//==============================================================================

	function delete()  {
		$q = "DELETE FROM `".$this->_sqlTable."` WHERE id=".$this->_id;

		$this->_db->query($q);
	}
	//==============================================================================

	private function insert()  {
		//echo "INSERT CATEGORY";

		$q = "INSERT INTO `".$this->_sqlTable."` (`parent_id`, `name`, `date`, `order_by`, `enabled`) VALUES('".$this->_parent."', '".addslashes($this->_name)."', NOW(), ".$this->_orderBy.", ".($this->_enabled?1:0).")";
		//echo $q;
		//exit;

		$this->_db->query($q);
		//if($this->_db->error()) {
		//	echo $this->_db->error();
		//	exit;
		//}

		$this->_id = mysql_insert_id();
	}
	//==============================================================================

	private function update()  {
		//echo "VsProductCategory::update()";

		$q = "UPDATE `".$this->_sqlTable."` SET `parent_id`='".$this->_parent."', `name`='".addslashes($this->_name)."', `date`=NOW(), `order_by`='".$this->_orderBy."', `enabled`='".$this->_enabled."' WHERE id=".$this->_id;

		//echo $q;

		$this->_db->query($q);
		//echo mysql_error();
	}
	//==============================================================================

	private function initPath($parentId=0) {
		if(!$parentId)
			return;

		$q = "SELECT * FROM `products_categories` WHERE id=".$parentId;
		$res = $this->_db->query($q);
		$rc = mysql_fetch_assoc($res);
		
		if($rc['parent_id'])
				$this->initPath($rc['parent_id']);

		if($rc) {
			array_push($this->_path, $rc);
			//array_push($this->_path, array('id'=>$rc['id'],'name'=>$rc['name']));
		}
	}
	//=============================================================================

	private function initChildCount() {
		$q = "SELECT id FROM `products_categories` WHERE parent_id=".$this->_id;
		$res = $this->_db->query($q);

		$this->_childCount = mysql_numrows($res);
	}
	//=============================================================================

	private function initProductCount() {
		$q = "SELECT id FROM `products` WHERE products_categories_id=".$this->_id;
		$res = $this->_db->query($q);

		$this->_productCount = mysql_numrows($res);
	}
	//=============================================================================

	private function initProductCountTotal($id=0) {
		if(!$this->_id)
			return;

		if(!$id)
			$id = $this->_id;
		
		$q = "SELECT id FROM `products_categories` WHERE parent_id=".$id;
		$res = $this->_db->query($q);
		while($r=mysql_fetch_object($res))
			$this->initProductCountTotal($r->id);

		$q = "SELECT id FROM `products` WHERE products_categories_id=".$id;
		$res = $this->_db->query($q);

		$this->_productCountTotal += @mysql_numrows($res);
	}
	//=============================================================================

};

?>
